package com.taotao.manage.service;

import com.github.abel533.entity.Example;
import com.github.abel533.mapper.Mapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.manage.pojo.BasePojo;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.ParameterizedType;
import java.util.Date;
import java.util.List;

public abstract class BaseService<T extends BasePojo> {
    @Autowired
    public Mapper<T> mapper;

    private Class<T> clazz;

    {
        ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();
        clazz = (Class<T>)type.getActualTypeArguments()[0];
    }

    public T queryById(Long id){
         /**
              * @author      ziye
              * @date        2018/4/20 19:47
              */
        return this.mapper.selectByPrimaryKey(id);
    }

    public List<T> queryAll(){
            /**
                 * @author      ziye
                 * @date        2018/4/20 19:48
                 */
        return this.mapper.select(null);
    }

    public T queryOne(T record){
          /**
               *  查询一条记录
               * @author      ziye
               * @date        2018/4/20 19:50
               */
        return this.mapper.selectOne(record);
    }

    public List<T> queryListByWhere(T record){
          /**
               *  根据条件查询
               * @author      ziye
               * @date        2018/4/20 19:55
               */
        return this.mapper.select(record);
    }

    public PageInfo<T> queryPageListByWhere(T record,Integer pageNum,Integer pageSize){
          /**
               *  分页查询
               * @author      ziye
               * @date        2018/4/20 21:16
               */
        PageHelper.startPage(pageNum,pageSize);
        List<T> list = this.mapper.select(record);
        return new PageInfo<>(list);
    }

    public PageInfo<T> queryPageListByWhereOrderBy(String orderByClause,Integer pageNum,Integer pageSize){
        /**
         *  分页查询并排序
         * @author      ziye
         * @date        2018/4/20 21:16
         */
        PageHelper.startPage(pageNum,pageSize);
        Example example = new Example(clazz);
        example.setOrderByClause(orderByClause);
        List<T> list = this.mapper.selectByExample(example);
        return new PageInfo<>(list);
    }
    
    public void save(T record){
          /**
               *  新增数据
               * @author      ziye
               * @date        2018/4/20 20:03
               */
          record.setCreated(new Date());
          record.setUpdated(record.getCreated());
        this.mapper.insert(record);
    }

    public Integer saveSelective(T record){
          /**
               *  新增数据,不添加null的字段
               * @author      ziye
               * @date        2018/4/20 20:08
               */
        record.setCreated(new Date());
        record.setUpdated(record.getCreated());
        return this.mapper.insertSelective(record);
    }

    public Integer update(T record){
          /**
               *  修改数据
               * @author      ziye
               * @date        2018/4/20 20:15
               */
        record.setUpdated(new Date());
        return this.mapper.updateByPrimaryKey(record);
    }

    public Integer updateSelective(T record){
        /**
         *  更新数据,不更新为null的字段
         * @author      ziye
         * @date        2018/4/20 20:15
         */
        record.setUpdated(new Date());
        record.setCreated(null);
        return this.mapper.updateByPrimaryKeySelective(record);
    }

    public Integer deleteById(Long id){
          /**
               *根据id删除
               * @author      ziye
               * @date        2018/4/20 20:20
               */
        return this.mapper.deleteByPrimaryKey(id);
    }

    public Integer deleteByIds(List<Object> ids,String property){
          /**
               *  根据多个id删除
               * @author      ziye
               * @date        2018/4/20 20:37
               */
        Example example = new Example(clazz);
        example.createCriteria().andIn(property,ids);
        return this.mapper.deleteByExample(example);
    }

    public Integer deleteByWhere(T record){
          /**
               *  根据多条件删除
               * @author      ziye
               * @date        2018/4/20 20:39
               */
        return this.mapper.delete(record);
    }
  /*
    10、	deleteByWhere
*/

}
